﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ITSupportPlatformWindowsService.Properties;
using System.Xml;
using ITSupportPlatformWindowsService.DAL;
namespace ITSupportPlatformWindowsService
{
    class SyncDataService
    {
        internal void DoSyncData()
        {
            ShartPointService service = new ShartPointService();
            service.Credentials = new System.Net.NetworkCredential(Settings.Default.ITUserName, Settings.Default.ITPassword, Settings.Default.ITDomain);
            service.Url = "http://zyinfo.founder.com/dept/IT/_vti_bin/Lists.asmx";

            XmlNode node = service.GetListItems("ERP实时公告栏", String.Empty, null, ViewFields, "0", QueryOptions, String.Empty);
            XmlDocument doc = new XmlDocument();
            XmlNamespaceManager nsp = new XmlNamespaceManager(doc.NameTable);
            nsp.AddNamespace("rs", "urn:schemas-microsoft-com:rowset");
            nsp.AddNamespace("z", "#RowsetSchema");
            XmlNodeList nodeList = node.SelectNodes("rs:data/z:row", nsp);

            FounderOAResourceGroupEntities context = new FounderOAResourceGroupEntities();

            foreach (XmlNode item in nodeList)
            {
                string id = item.Attributes["ows_id"].Value;
                string title = item.Attributes["ows_Title"].Value;
                string body = item.Attributes["ows_Body"].Value;
                string createTime = item.Attributes["ows_Created"].Value;
                string users = item.Attributes["ows_relevance"].Value;

                context.ITSupport_Notices.AddObject(new ITSupport_Notices()
                {
                    Id = new Guid(),
                    Title = title,
                    NoticeContent = body,
                    CreateTime = DateTime.Parse(createTime),
                    SendUsers = users,
                    SPId = id
                });
            }
        }

        static XmlNode QueryOptions
        {

            get
            {

                XmlDocument _camlDoc = new XmlDocument();

                XmlElement queryOptions = _camlDoc.CreateElement("QueryOptions");

                queryOptions.InnerXml = "<ExpandUserField>True</ExpandUserField>";

                return queryOptions;

            }

        }

        static XmlNode ViewFields
        {

            get
            {
                XmlDocument _camlDoc = new XmlDocument();
                XmlElement viewFields = _camlDoc.CreateElement("ViewFields");

                System.Text.StringBuilder viewFieldsSB = new System.Text.StringBuilder();

                #region View Fields

                viewFieldsSB.Append(" <FieldRef Name=\"ID\" />");

                viewFieldsSB.Append(" <FieldRef Name=\"Title\" />");

                viewFieldsSB.Append(" <FieldRef Name=\"Body\" />");

                viewFieldsSB.Append(" <FieldRef Name=\"Created\" />");

                viewFieldsSB.Append(" <FieldRef Name=\"relevance\" />");

                #endregion

                string sResult = viewFieldsSB.ToString();

                viewFields.InnerXml = viewFieldsSB.ToString();

                return viewFields;

            }
        }
    }
}
